package com.hdy.orderforthree.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdy.net.order.bean.TablesBean;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;

import java.util.List;

import butterknife.BindView;

public class TablesAdapter extends BaseAdapter{

    static class ViewHolder extends BaseAdapter.ViewHolder{

        @BindView(R2.id.tables_image)
        ImageView mTablesImage;

        @BindView(R2.id.tables_selected_image)
        ImageView mSelectedImage;

        @BindView(R2.id.tables_name)
        TextView mTablesName;

        public ViewHolder(View view){
            super(view);
        }
    }

    public TablesAdapter(List list) {
        super(R.layout.table_info_item, list);
    }

    @Override
    public BaseAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false);
        RecyclerView.ViewHolder holder = new ViewHolder(view);
        return (BaseAdapter.ViewHolder) holder;
    }

    @Override
    public void onBindViewHolder(BaseAdapter.ViewHolder holder, final int position) {
        TablesBean table = (TablesBean) mList.get(position);

        ViewHolder curHolder = (ViewHolder) holder;
        curHolder.mTablesImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notifySelectedChanged(position);

                if (mOnItemClickListener  != null) {
                    mOnItemClickListener.OnItemClick(v, holder, position);
                }
            }
        });

        if (table.getState() == 1) {
            curHolder.mTablesImage.setBackgroundResource(R.drawable.occupy);
        } else {
            curHolder.mTablesImage.setBackgroundResource(R.drawable.idle);
        }

        if (mSelectedPosition == position) {
            curHolder.mSelectedImage.setVisibility(View.VISIBLE);
        } else {
            curHolder.mSelectedImage.setVisibility(View.GONE);
        }

        curHolder.mTablesName.setText(table.getName());
    }
}
